package com.encryption.logic.impl;

import java.util.List;

import com.encryption.dao.Condition;

public class ConditionSqlParser {
	
	public static String parse(List<Condition> condList){
		String condSql = "";
		for (Condition cond : condList){
			String operation = cond.getOperation();
			Object value = cond.getValue();
			String valueStr = value != null ? value.toString() : "";
			String sqlStr = "";
			if(Condition.LIKE.equalsIgnoreCase(operation)){
				sqlStr = cond.getCode() + " " + Condition.LIKE + " '%"+ valueStr +"%' "; 
			}else if(Condition.BETWEEN.equalsIgnoreCase(operation)){
				int index = valueStr.indexOf(",");
				if(index == -1)
					continue;
				
				String value1 = valueStr.substring(0, index);
				String value2 = valueStr.substring(index + 1);
				sqlStr = cond.getCode() + " " + Condition.BETWEEN + " " + value1 + " and " + value2;  
			}else{
				sqlStr = cond.getCode() + " " + operation + " " + valueStr;
			}
			
			condSql += " and (" + sqlStr + ") ";
		}
		
		return condSql;
	}
	
}
